<?php
namespace app\index\controller;
use think\Db;
use request;
class Api

// API接口

{
    // 游戏列表
    public function login()
    {
        $data = input();
        $ifexit = Db::name("users")->where("username",$data['phone'])->find();
        
        $infonull['id'] = '';
        $infonull['username'] = '';
        $infonull['nickname'] = '';
        $infonull['coin'] = '';
        $infonull['money'] = '';
        $infonull['token'] = '';

        if(!$ifexit){
            $rs['code']=1002;
            $rs['msg']='用户不存在';
            $rs['info']=$infonull;
            return json($rs);
            // return $rs;
        }
        


        $comparepass = comparepass($data['password'],$ifexit['password']);
    
        if(!$comparepass){
            $rs['code']=1003;
            $rs['msg']='密码错误';
            $rs['info']=$infonull;
            return json($rs);    
        }
        if($ifexit['status']==1){
            $rs['code']=1004;
            $rs['msg']='账号被锁定，暂时无法登录';
            $rs['info']=$infonull;
            return json($rs);
        }
        
        $user = getuserinfo3($data['phone']); 
        
        $user['token'] = updatetoken($data['phone']);
        $rs['code']=1;
        $rs['msg']='登录成功';
        $rs['info']=$user;
        
        return json($rs);
    }
    // 用户注册
    public function reg(){
        $data = input();
        if($data['phone']==''){
            $rs['code']=1005;
            $rs['msg']='请输入手机号';
            $rs['info']=[];
            return json($rs);
        }
        if($data['password']==''){
            $rs['code']=1006;
            $rs['msg']='请输入密码';
            $rs['info']=[];
            return json($rs);
        }
        if($data['password2']==''){
            $rs['code']=1007;
            $rs['msg']='请再次输入密码';
            $rs['info']=[];
            return json($rs);
        }
        if($data['password2']!=$data['password']){
            $rs['code']=1007;
            $rs['msg']='两次密码不一致';
            $rs['info']=[];
            return json($rs);
        }
        if($data['invitecode']==''){
            $rs['code']=1008;
            $rs['msg']='请输入邀请码';
            $rs['info']=[];
            return json($rs);
        }
        // 检查邀请码
        $checkcode = checkcode($data['invitecode']);
        if(!$checkcode){
            $rs['code']=1015;
            $rs['msg']='邀请码错误';
            $rs['info']=[];
            return json($rs);
        }
        $create = creteuseraccount($data['phone'],$data['password'],$data['invitecode']);
        if($create==1038){
            $rs['code']=1009;
            $rs['msg']='该手机号已注册';
            $rs['info']=[];
            return json($rs);
        }
       
        $rs['code']=1;
        $rs['msg']='注册成功';
        $rs['info']=$create;
        return json($rs);
        
    }

    public function getuserinfo(){
        $data = input();
        $checktoken = checktoken($data['uid'],$data['token']);
        if($checktoken !=1 ){
            $rs['code']=700;
            $rs['msg']='登录过期，请重新登录';
            $rs['info']=[];
            return json($rs);
        }
     
        $rs['code']=1;
        $rs['msg']='success';
        $rs['info']=getuserinfo($data['uid']);
  
        return json($rs);
    }

    public function trans(){
        $data = input();
        $checktoken = checktoken($data['uid'],$data['token']);
        if($checktoken !=1 ){
            $rs['code']=700;
            $rs['msg']='登录过期，请重新登录';
            $rs['info']=[];
            return json($rs);
        }
        if(!$data['touid']){
            $rs['code']=1025;
            $rs['msg']='请输入接收人ID';
            $rs['info']=[];
            return json($rs);
        }
        if(!$data['amount']){
            $rs['code']=1025;
            $rs['msg']='请输入发送数量';
            $rs['info']=[];
            return json($rs);
        }
        $touser = Db::name("users")->where("id",$data['touid'])->find();
        if(!$touser){
            $rs['code']=1028;
            $rs['msg']='接收人不存在';
            $rs['info']=[];
            return json($rs); 
        }
        $user = Db::name("users")->where("id",$data['uid'])->find();
        if($user['score']<$data['amount']){
            $rs['code']=1029;
            $rs['msg']='余额不足';
            $rs['info']=[];
            return json($rs);
        }
        // 判断是否一条推荐线上的
        $agentlist = [];
        $agents = getallagents($data['uid'],$agentlist);
        // var_dump($agents);die;
        if (in_array($data['touid'], $agents)) {
          
        }else{
            $rs['code']=1029;
            $rs['msg']='非同一团队禁止转增金币';
            $rs['info']=[];
            return json($rs);
        }

        $a1 = upusercoin($data['uid'],$data['amount'],2,'转增金币给ID:'.$data['touid']);
        $a2 = upusercoin($data['touid'],$data['amount'],1,'收到ID:'.$data['uid'].'转增金币');

        if($a1 && $a2){
            $rs['code']=1;
            $rs['msg']='转增成功';
            $rs['info']=[];
            return json($rs);
        }else{
            $rs['code']=1030;
            $rs['msg']='转增失败';
            $rs['info']=[];
            return json($rs);
        }

    }

    public function getteams(){
        $data = input();
        $checktoken = checktoken($data['uid'],$data['token']);
        if($checktoken !=1 ){
            $rs['code']=700;
            $rs['msg']='登录过期，请重新登录';
            $rs['info']=[];
            return json($rs);
        }
        $xiaji = Db::name("agents")->where("oneuid",$data['uid'])->select();
        if($xiaji){
            foreach($xiaji as $k =>$v){
                $userinfo = getuserinfo($v['uid']);
                $userinfo['score']= handlewan($userinfo['score']);
                $new[]=$userinfo;
            }
            $rs['code']=1;
            $rs['msg']='成功';
            $rs['info']=$new;
            return json($rs);

        }else{
           
            $rs['code']=105;
            $rs['msg']='暂无数据';
            $rs['info']=[];
            return json($rs);
        }
        
    }

    // 获取金币消费记录
    public function coinrecord(){
        $data=input();
        $username = input('username') ? input('username') : '';
        $page = input('page') ? input('page') : '';
        $sign = input('sign') ? input('sign') : '';
        if(!$username){
            $rs['code']=1085;
            $rs['msg']='用户名不能为空';
            $rs['info']=[];
            return json($rs);
        }
        if(!$page){
            $rs['code']=1086;
            $rs['msg']='page不能为空';
            $rs['info']=[];
            return json($rs);
        }
        if(!$sign){
            $rs['code']=1085;
            $rs['msg']='签名不能为空';
            $rs['info']=[];
            return json($rs);
        }
        $data=[
            'username'=>$username,
        ];
        $sign = makesign($data);
        // var_dump($sign);die;
        $checksign =  checksign($data,$sign);
        if(!$checksign){
            return json(['code'=>1025,'msg'=>'签名错误']);
        }
        $user = Db::name("users")->where("username",$username)->find();
        if(!$user){
            $rs['code']=1095;
            $rs['msg']='用户名错误';
            $rs['info']=[];
            return json($rs);
        }
        $record = Db::name('usercoin_record')->where('uid',$user['id'])->order('id desc')->page($page,10)->select();
        $rs['code']=1;
            $rs['msg']='获取成功';
            $rs['info']=$record;
            return json($rs);
        }


}